package com.lungle.warmdiary.app.view.wallet.fragment;

import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.AppContext;
import com.lungle.warmdiary.app.api.remote.ApiFactory;
import com.lungle.warmdiary.app.base.BaseDetailFragment;
import com.lungle.warmdiary.app.base.BaseSubscriber;
import com.lungle.warmdiary.app.model.Result;
import com.lungle.warmdiary.app.model.response.TradeRecoder;
import com.lungle.warmdiary.app.model.response.WithdrawRecoder;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by God on 2016/8/14.
 */
public class TradeDetailFragment extends BaseDetailFragment {
    private static final String TRADE_DETAIL_SCREEN = "trade detail screen";
    public static final String ARGS_TRADE = "trade";
    public static final String ID_TRADE = "trade_id";

    @Bind(R.id.tv_trade_way)
    TextView mTvTradeWay;
    @Bind(R.id.tv_trade_sum)
    TextView mTvTradeSum;
    private TradeRecoder mTradeRecoder;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);

        initView(view);
        initData();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TRADE_DETAIL_SCREEN);
        MobclickAgent.onResume(getActivity());

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TRADE_DETAIL_SCREEN);
        MobclickAgent.onPause(getActivity());

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_trade_detail;
    }

    @Override
    public void initView(View view) {
        ButterKnife.bind(this, view);
    }

    @Override
    public void initData() {
        mTradeRecoder = (TradeRecoder) getActivity().getIntent().getSerializableExtra(ARGS_TRADE);
        if (mTradeRecoder == null) {
            String id = getActivity().getIntent().getStringExtra(ID_TRADE);
            getDetailInfo(id);
            return;
        } else {
            fillUI();
        }


    }

    private void getDetailInfo(String id) {
        showWaitDialog();
        ApiFactory.getUserApi().getWithdrawDetail(id).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<Result<WithdrawRecoder>>(getActivity()) {
                    @Override
                    public void onCompleted() {
                        hideWaitDialog();
                    }

                    @Override
                    public void onNext(Result<WithdrawRecoder> result) {
                        WithdrawRecoder mWithdrawRecoder = result.getData();
                        if (mWithdrawRecoder == null) return;
                        if (mWithdrawRecoder.getStatus() == WithdrawRecoder.STATE_NEW ||
                                mWithdrawRecoder.getStatus() == WithdrawRecoder.STATE_PASS || mWithdrawRecoder.getStatus() == WithdrawRecoder.STATE_FINISH) {//"-"支出
                            if (mWithdrawRecoder.getAmount() < 0) {
                                mTvTradeSum.setText(Html.fromHtml("<font color=#008000>" + mWithdrawRecoder.getAmount() + "</font><font color=#333333>元</font>"));
                            } else {
                                mTvTradeSum.setText(Html.fromHtml("<font color=#008000>-" + mWithdrawRecoder.getAmount() + "</font><font color=#333333>元</font>"));
                            }
                        } else {//+
                            mTvTradeSum.setText(Html.fromHtml("<font color=#f94848>+" + mWithdrawRecoder.getAmount() + "</font><font color=#333333>元</font>"));
                        }

                        mTvTradeWay.setText(WithdrawRecoder.STATE_MAP.get(mWithdrawRecoder.getStatus()));

                    }

                    @Override
                    public void onError(Throwable e) {
                        hideWaitDialog();
                        AppContext.showToast("获取失败");
                    }
                });
    }

    private void fillUI() {
        if (mTradeRecoder == null) return;
        if (mTradeRecoder.getType() == TradeRecoder.TYPE_WITHDRAW) {//"-"支出
            if (mTradeRecoder.getAmount() < 0) {
                mTvTradeSum.setText(Html.fromHtml("<font color=#008000>" + mTradeRecoder.getAmount() + "</font><font color=#333333>元</font>"));
            } else {
                mTvTradeSum.setText(Html.fromHtml("<font color=#008000>-" + mTradeRecoder.getAmount() + "</font><font color=#333333>元</font>"));
            }
        } else {//+
            mTvTradeSum.setText(Html.fromHtml("<font color=#f94848>+" + mTradeRecoder.getAmount() + "</font><font color=#333333>元</font>"));
        }

        mTvTradeWay.setText(TradeRecoder.TYPE_MAP.get(mTradeRecoder.getType()));
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
